package org.qianye.soft.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * SpringApplicationContext
 *
 * @author dragon
 */
public class SpringApplicationContext {

	/**
	 * HSQL_DB
	 */
	public static final String HSQL = "applicationContext_hsqldb.xml";
	/**
	 * MYSQL_DB
	 */
	public static final String MYSQL = "applicationContext_mysql.xml";
	/**
	 * Spring框架应用上下文对象
	 */
	private static ApplicationContext factory = null;

	/**
	 * 获得Spring框架应用上下文对象
	 *
	 * @param type
	 */
	public static synchronized void init(String type) {
		if (factory != null) {
			throw new IllegalStateException("SpringApplicationContext has initiated!");
		}
		String app_ctx_xml = (type.equals(MYSQL) ? MYSQL : HSQL);
		try {
			System.out.println("******init spring application context");
			factory = new ClassPathXmlApplicationContext(new String[] { app_ctx_xml });
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获得Spring框架应用上下文对象
	 *
	 * @return ApplicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return factory;
	}

	/**
	 * Destroy SpringApplicationContext
	 */
	public static void destroy() {
		System.out.println("******destroy spring application context");
		SpringApplicationContext.factory = null;
	}

}
